package com.simple.mipc.cast;

import com.simple.mipc.annotation.Param;

public class AutoCast<T> implements ParamCast<T> {

    @Override
    public T castTo(Object obj, Class<T> clazz) {
        boolean result = check(obj, clazz);
        if (result) {
            return (T) obj;
        } else {
            throw new IllegalArgumentException(String.format("不支持的类型转换，请使用主机：%s，指定类型转换器，类型转换器必须是：%s类型", Param.class, ParamCast.class));
        }
    }

    @Override
    public boolean check(Object obj, Class<T> clazz) {
        return clazz.isInstance(obj);
    }
}
